﻿# game.py: Alien Xenocide main module
# 04/06/09
# Copyright(c) 2009 Victor Freire e Cássio Espindola. All rights reserved.
# Python 2.6.2

import pygame
import Sprites
import Gameobjects
import Timer
import Input
import Raycaster

class FPS_counter:
    def __init__(self):
        self.frames = 0
    def average(self):
        return self.frames / (pygame.time.get_ticks() / 1000.)


class Game:
    def __init__(self, screen_width=640, screen_height=480):
        ''' Initializes the game data and modules. '''

        # The game window
        self.GAME_VERSION = "0.02"
        self.WINDOW_TITLE = 'Alien Xenocide - Version %s' % self.GAME_VERSION

        self.screen_width = screen_width
        self.screen_height = screen_height
        self.screen_bpp = 32

        # The rendering window
        self.projection_w = 640
        self.projection_h = 480


        # Pygame initialization
        pygame.init()

        # Pygame display configuration
        try:
            self.screen = pygame.display.set_mode((self.screen_width, self.screen_height), 0 , self.screen_bpp)
        except pygame.error, msg:
            print "Error: failed to initialize display -", msg
        pygame.display.set_caption(self.WINDOW_TITLE)    


        # Raycasting renderer
        self.Renderer = Raycaster.RendererClass(self.projection_w, self.projection_h)

        self.player = Gameobjects.Player(self.Renderer.CUBE_SIZE*1.5, self.Renderer.CUBE_SIZE*2.5, 45)

        world = [['1','1','1','2','1','2','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'],
                 ['1','0','0','0','0','0','0','1','0','0','0','0','0','0','0','0','0','0','0','0','1'],
                 ['1','0','0','0','0','0','0','0','0','0','0','1','1','1','1','1','1','0','0','0','1'],
                 ['1','0','0','0','0','0','0','0','0','0','0','1','0','0','0','0','1','0','0','0','1'],
                 ['1','0','0','0','0','0','0','1','0','0','0','1','0','0','0','0','1','0','0','0','1'],
                 ['1','1','1','1','1','1','1','1','1','1','1','1','0','0','0','0','1','0','0','0','1'],
                 ['1','0','0','0','0','0','0','0','0','0','0','0','0','0','1','1','1','0','0','0','1'],
                 ['1','0','0','0','0','0','0','0','0','0','1','1','1','0','1','0','0','0','0','0','1'],
                 ['1','0','0','0','0','0','0','0','0','0','1','0','1','0','1','0','0','0','0','0','1'],
                 ['1','0','0','0','0','1','1','1','1','1','1','0','1','0','1','0','0','1','1','1','1'],
                 ['1','0','0','0','0','1','0','1','0','1','0','0','1','0','1','0','0','1','0','0','1'],
                 ['1','0','0','0','0','1','0','0','0','0','0','0','1','1','1','0','0','1','1','1','1'],
                 ['1','0','0','0','0','1','0','1','0','0','0','0','0','0','0','0','0','0','0','0','1'],
                 ['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1']]

        self.Renderer.load_world(world)

        # Sprites Manager
        self.Sprites = Sprites.Sprites()
        self.Sprites.load_sprites()
        
        # Game timer
        self.Timer = Timer.Timer()
        self.mult_factor = 0

        # Game input
        self.Input = Input.Input()
               

    def shutdown(self):
        pygame.quit()
        

    def main(self):
        while self.handle_input():
            self.screen.fill((0, 0, 0)) #clear screen
            self.Renderer.render_scene(self.screen, self.player)
            self.draw_hud()
            pygame.display.flip()
            self.Timer.update()
            self.mult_factor = self.Timer.get_mult_factor()
            
            

    def draw_hud(self):
        pass
        #self.screen.blit(self.Sprites.get_sprite('ui', 'barra_status'), (0, self.screen_height - 120))
                            
    def handle_input(self):
        keep_running = self.Input.get_input()
        
        if self.Input.check(pygame.K_ESCAPE):
            keep_running = False
            
        if self.Input.check(pygame.K_UP):
            self.player.move_forward(self.mult_factor)

        if self.Input.check(pygame.K_DOWN):
            self.player.move_backward(self.mult_factor)

        if self.Input.check(pygame.K_LEFT):
            if self.Input.check(pygame.K_LALT):
                self.player.strafe_left(self.mult_factor)
            else:
                self.player.turn_left(self.mult_factor)

        if self.Input.check(pygame.K_RIGHT):
            if self.Input.check(pygame.K_LALT):
                self.player.strafe_right(self.mult_factor)
            else:
                self.player.turn_right(self.mult_factor)
                                
        return keep_running                
                  


def main():
    game = Game() # creates the game object and initializes the modules
    game.main()   # starts the game
    game.shutdown()   # cleans up before exitting
    print "Thanks for playing Alien Xenocide."    

main()
